/**
 * Created by hcadmin on 16/5/20.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function($,IO,jui,upui,up,bootbox,definebt) {
    $(function () {
        $('.lookservicepeople').click(function(){
            var company_id=$("#company_id_id").val();
            location.href='/96053/service_people?company_id='+company_id;
        });
        //省市区详情
        Get_detail();

        $(".selectpro").on("change",function(){
            //console.log($(this).val());
            document.reg_testdate.selectcity.innerHTML='请选择市';
            document.reg_testdate.selectcity.options.add(new Option("请选择市","-1"));

            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));

            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == $(this).val()){
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate.selectcity.options.add(new Option(city[x].name,city[x].code));
                    }
                }
            }
        });

        $(".selectcity").on("change",function(){
            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));
            var province = $(".selectpro option:selected").val();
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == province){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if ($(".selectcity option:selected").text()==city[x].name){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate.selectarea.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }
        });


        //修改公司
        $(".btnEdit").click(function(){
            var company_name = $("#company_name").val();
            var company_phone = $("#company_phone").val();
            var province_id = $(".selectpro option:selected").attr('data-id');
            var city_id = $(".selectcity option:selected").val();
            var area_id = $(".selectarea option:selected").val();
            var address=$("#address").val();
            var leader_name=$("#leader_name").val();
            var leader_tele=$("#leader_tele").val();
            var yuesao='';
            var company_id=$("#company_id_id").val();
            if(company_name.length<=0){
                bootbox.alert('请填写公司名称');
                return;
            }
            if(company_phone.length<=0){
                bootbox.alert('请填写公司电话');
                return;
            }
            if(address.length<=0){
                bootbox.alert('请填写公司地址');
                return;
            }
            if(leader_name.length<=0){
                bootbox.alert('请填写公司负责人姓名');
                return;
            }
            if(leader_tele.length<=0){
                bootbox.alert('请填写公司负责人联系电话');
                return;
            }
            if(province_id==-1){
                bootbox.alert('请选择省');
                return;
            }
            if(city_id=="-1"){
                bootbox.alert('请选择市');
                return;
            }
            if(area_id=="-1"){
                bootbox.alert('请选择区');
                return;
            }
            var params={
                company_name:company_name,
                company_phone:company_phone,
                address:address,
                leader_name:leader_name,
                leader_tele:leader_tele,
                province_id:province_id,
                city_id:city_id,
                area_id:area_id,
                yuesao:yuesao,
                company_id:company_id
            };
            IO.get("/api/homemaking_company_edit", params, function (data) {
                if (data.code == 0) {
                    bootbox.alert('公司修改成功');
                }
                else {
                    bootbox.alert('公司修改失败');
                }
            })
        });

    });
    function Get_detail(){
        var provinceList = JSON.parse($("#province").val());
        var province_id = $("#province_id").val();
        var city_id = $("#city_id").val();
        var area_id = $("#area_id").val();
        console.log('province_id',province_id);
        console.log('city_id',city_id);
        console.log('area_id',area_id);
        document.reg_testdate.selectcity.innerHTML='请选择市';
        document.reg_testdate.selectcity.options.add(new Option("请选择市","请选择市"));
        for (var obj in provinceList){
            if(provinceList[obj].code == province_id){
                for (var x in provinceList[obj].cityList){
                    var city = provinceList[obj].cityList;
                    document.reg_testdate.selectcity.options.add(new Option(city[x].name,city[x].code));
                }
            }
        }

        //获取区的下拉框选项
        document.reg_testdate.selectarea.innerHTML='请选择区';
        document.reg_testdate.selectarea.options.add(new Option("请选择区","请选择区"));
        for (var  obj in provinceList){
            if(provinceList[obj].code == province_id){
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                var city = provinceList[obj].cityList;
                for (var x in city){
                    if (city[x].code == city_id){
                        var area = city[x].areaList;
                        for (var y in area){
                            document.reg_testdate.selectarea.options.add(new Option(area[y].name,area[y].code));
                            //console.log("城市们",area[y].name);
                        }
                    }
                }
            }
        }
        //下拉框选中该医院所在的省市区
        var provinces = $(".selectpro");
        var citys = $(".selectcity");
        var areas = $(".selectarea");
        for(var x=1;x<provinces[0].length;x++ ){
            if($(provinces[0][x]).attr('data-id')==province_id){
                provinces[0][x].selected = true;
                continue;
            }
        }

        for(var y=1;y<citys[0].length;y++ ){
            if(citys[0][y].value==city_id){
                citys[0][y].selected = true;
                continue;
            }
        }
        for(var z=1;z<areas[0].length;z++ ){
            if(areas[0][z].value==area_id){
                areas[0][z].selected = true;
                continue;
            }
        }

    };
});